from flask import url_for
from flask_restful import Resource

from restful.api__.public__.info.atomic.PersonalInfo import PersonalInfo
from restful.api__.public__.info.atomic.PostAllComments import PostAllComments
from restful.api__.public__.info.atomic.PostAllLikes import PostAllLikes
from restful.api__.public__.info.atomic.PostInfoPublished import PostInfoPublished
from restful.api__.public__.info.atomic.RoleInfo import RoleInfo
from restful.api__.public__.info.atomic.UserAllFans import UserAllFans
from restful.api__.public__.info.atomic.UserAllPostsPublished import UserAllPostsPublished
from restful.api__.public__.info.atomic.UserAllPostsPublishedGetLikes import UserAllPostsPublishedGetLikes
from restful.api__.public__.info.atomic.UserAlreadyFollow import UserAlreadyFollow
from restful.api__.public__.info.atomic.UserAlreadyLike import UserAlreadyLike
from restful.api__.public__.info.atomic.UserName import UserName
from restful.api__.session import login_required_current_user_generate, pid_required_target_post_generate
from restful.response import ResponseSuccess


class PostInfoPublishedDetail(Resource):
    @login_required_current_user_generate
    @pid_required_target_post_generate
    def get(self,current_user,target_post):
        post_data = PostInfoPublished().get(pid=target_post.id)[0]['data']

        raw_comments = PostAllComments().get(pid=target_post.id)[0]['data']
        comments = []

        for c in raw_comments:
            if c['uid'] == current_user.id:
                c['delete_url'] = url_for('commentorcancel', cid=c['cid'])
            comments.append(c)

        data={
            'fans':UserAllFans().get(uid=current_user.id)[0]['data'],
            'user_get_likes':UserAllPostsPublishedGetLikes().get(uid=current_user.id)[0]['data']['count'],
            'published':UserAllPostsPublished().get(uid=current_user.id)[0]['data'],
            'this_post_get_likes':PostAllLikes().get(pid=target_post.id)[0]['data'],
            'comments': comments,
            'personal':PersonalInfo().get(uid=target_post.uid)[0]['data'],
            'role':RoleInfo().get(uid=target_post.uid)[0]['data']['role'],
            'admin': RoleInfo().get(uid=target_post.uid)[0]['data']['admin'],
            'username':UserName().get(uid=target_post.uid)[0]['data']['username'],
            'post':post_data,
            'already_like':UserAlreadyLike().get(pid=post_data['pid'])[0]['data']['already_like'],
            'already_follow':UserAlreadyFollow().get(uid=post_data['uid'])[0]['data']['already_follow'],
            'urls':{
                'like_or_cancel':url_for('likeorcancel',pid=post_data['pid']),
                'follow_or_cancel':url_for('followorcancel',uid=post_data['uid']),
                'comment_or_cancel':url_for('commentorcancel',pid=post_data['pid']),
            }
        }
        return ResponseSuccess(data=data)
